//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state:"",
    pageIndex: 1,
    freeOrderList: [],
    // 
    uid:"",
    userType:"",
    // 
    top: [{
      'title': '全部',
      'active': true,
      'type': 0
    },
    {
      'title': '分期订单',
      'active': false,
      'type': 1,
    }
    ],
    orderType: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })

    this.GetMyFreeOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetMyFreeOrderList();
  },

  // 顶部
  topTap: function (e) {
    var top = this.data.top;
    var orderType = e.currentTarget.dataset.type;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].type == orderType) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      orderType: orderType
    })
    this.data.pageIndex = 1;
    this.data.freeOrderList = [];
    this.GetMyFreeOrderList();
  },
  // 获取订单列表
  GetMyFreeOrderList: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var freeOrderList = this.data.freeOrderList;
    var orderType = this.data.orderType;
    if (pageIndex == 1){
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyFreeOrderList&UID=" + uid + "&PageSize=10&PageIndex=" + pageIndex + "&OrderType=" + orderType),
      success: function(res) {
        // console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          json[i].FinishTime = json[i].FinishTime.replace('T', ' ');
          freeOrderList.push(json[i])
        }
        that.setData({
          freeOrderList: freeOrderList
        })
        wx.hideLoading();
        console.log(freeOrderList);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })
  },
  // 
})